/*
 * Projet industriel VAMP 2012 
 */
package com.vampmobile.android.activity;

import com.vampmobile.R;
import com.vampmobile.android.VampCommon.Commons.KINDS;
import com.vampmobile.android.db.VampDatabase;
import com.vampmobile.android.db.VampREST;
import com.vampmobile.android.db.Poi;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.CheckBox;
import android.widget.Toast;

/**
 * class to display profile of user FO && AM
 */
public class PoiNew  extends Activity {

	public static final String TAG = "Add POI";
	private ImageView mHomeTitle;
	private Button mBtnSave;
	private Button mBtnCancel;
	private ImageView mHomeButton;
	private ImageView mBackButton;
	private ImageView mSearchButton;
	private ImageView mQuitButton;
	private ImageView mAccountButton;
	private TextView mBeginDate; 
	private TextView mEndDate; 
	private ImageButton mBeginDatePicker; 
	private ImageButton mEndDatePicker; 
	private int mBeginYear; 
	private int mBeginMonth; 
	private int mBeginDay; 
	private int mEndYear; 
	private int mEndMonth; 
	private int mEndDay; 
	static final int BEGIN_DATE_DIALOG_ID = 0;
	static final int END_DATE_DIALOG_ID = 1;
	

	/*
	 * click listener
	 */
	// ===========================================================================================//
	class ItemClickListener implements OnClickListener {

		private KINDS kind = null;

		// case button
		public ItemClickListener(KINDS kind) {
			super();
			this.kind = kind;
		}


		public void onClick(View mView) {
			switch (kind) {
			case TOOLBAR:
				FrameLayout toolbar = (FrameLayout) findViewById(R.id.toolbar); 
				if (toolbar.getVisibility()!=View.VISIBLE)
					toolbar.setVisibility(View.VISIBLE);
				else
					toolbar.setVisibility(View.GONE);
				break;
			case QUIT:
				android.os.Process.killProcess(android.os.Process.myPid());
				java.lang.System.exit(0);
				finish();
				break;
			case SEARCH:
				break;
			case BACK:
				goPoi();
				break;
			case PROFILE_TITLE:
				goProfile();
				break;
			case HOME_TITLE:
				goHome();
				break;
			case SAVE:
				save();
				break;
			case CANCEL:
				goPoi();
				break;
			case BEGINDATE:
				showDialog(BEGIN_DATE_DIALOG_ID); 
				break;
			case ENDDATE:
				showDialog(END_DATE_DIALOG_ID);
				break;
			}

		}

	}

	private DatePickerDialog.OnDateSetListener mDateBeginSetListener = new DatePickerDialog.OnDateSetListener() {
		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
			mBeginYear = year;
			mBeginMonth = monthOfYear;
			mBeginDay = dayOfMonth;
			updateBeginDate();
		}
	};
	private DatePickerDialog.OnDateSetListener mDateEndSetListener = new DatePickerDialog.OnDateSetListener() {
		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
			mEndYear = year;
			mEndMonth = monthOfYear;
			mEndDay = dayOfMonth;
			updateEndDate();
		}
	};

	// ===========================================================================================//

	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setTitle(TAG);
		updateView();

	}

	private void updateBeginDate() {
		this.mBeginDate.setText(new StringBuilder().append(mBeginYear)
				.append("-")
				// Month is 0 based so add 1
				.append(mBeginMonth + 1).append("-").append(mBeginDay)
				.append(" "));
	}

	private void updateEndDate() { 
	    this.mEndDate.setText( 
	        new StringBuilder().append(mEndYear) 
			.append("-")
			// Month is 0 based so add 1
			.append(mEndMonth + 1).append("-").append(mEndDay)
			.append(" "));
	} 
	@Override 
	protected Dialog onCreateDialog(int id) { 
	   switch (id) { 
	   case BEGIN_DATE_DIALOG_ID: 
	      return new DatePickerDialog(this, 
	                mDateBeginSetListener, 
	                mBeginYear, mBeginMonth, mBeginDay); 
	   case END_DATE_DIALOG_ID: 
		      return new DatePickerDialog(this, 
		                mDateEndSetListener, 
		                mEndYear, mEndMonth, mEndDay); 
	   } 
	   return null; 
	} 

	private void updateView() {
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.poi_add_new);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);
		((TextView)findViewById(R.id.activityTitle)).setText(TAG);
	    mBeginDate = (TextView) findViewById(R.id.poi_begin_date_value);         
	    mBeginDatePicker = (ImageButton) findViewById(R.id.poi_begin_date); 
	    mBeginDatePicker.setOnClickListener(new ItemClickListener(KINDS.BEGINDATE));
	    mEndDate = (TextView) findViewById(R.id.poi_end_date_value);         
	    mEndDatePicker = (ImageButton) findViewById(R.id.poi_end_date); 
	    mEndDatePicker.setOnClickListener(new ItemClickListener(KINDS.ENDDATE));
		// button of title bar
		mHomeTitle = (ImageView) findViewById(R.id.home_title);
		mHomeTitle.setOnClickListener(new ItemClickListener(KINDS.TOOLBAR));
		// button of toolbar
		mHomeButton = (ImageView) findViewById(R.id.home);
		mHomeButton.setVisibility(View.GONE);
		mBackButton = (ImageView) findViewById(R.id.back_button);
		mBackButton.setOnClickListener(new ItemClickListener(KINDS.BACK));
		mSearchButton = (ImageView) findViewById(R.id.search);
		mSearchButton.setVisibility(View.GONE);
		mQuitButton = (ImageView) findViewById(R.id.quit);
		mQuitButton.setOnClickListener(new ItemClickListener(KINDS.QUIT));
		mAccountButton = (ImageView) findViewById(R.id.account);
		mAccountButton.setOnClickListener(new ItemClickListener(KINDS.PROFILE_TITLE));		
		mBtnSave = (Button) findViewById(R.id.save);
		mBtnSave.setOnClickListener(new ItemClickListener(KINDS.SAVE));
		mBtnCancel = (Button) findViewById(R.id.cancel);
		mBtnCancel.setOnClickListener(new ItemClickListener(KINDS.CANCEL));
	}

	@Override
	public void onResume() {
		super.onResume();
		onNewIntent(getIntent());
	}

	@Override
	public void finish() {
		super.finish();
	}

	@Override
	public void onPause() {
		super.onPause();
	}

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
	}
	
	public void goProfile() {
		Intent myIntent = new Intent();
		myIntent.setClass(getApplicationContext(), PoiNew.class);
		myIntent.setAction(Intent.ACTION_VIEW);
		finish();
		startActivity(myIntent);		
	}
	
	public void goHome() {
		Intent i = new Intent();
		i.setClass(getApplicationContext(), Home.class);
		i.setAction(Intent.ACTION_VIEW);
		finish();
		startActivity(i);
	}

	public void goPoi() {
		Intent i = new Intent();
		i.setAction(Intent.ACTION_VIEW);
		i.setClass(getApplicationContext(), com.vampmobile.android.activity.Poi.class);
		finish();
		startActivity(i);
	}
	
	private void save() {
		EditText line1 = (EditText)findViewById(R.id.poi_address1);
		EditText line2 = (EditText)findViewById(R.id.poi_address2);
		EditText city = (EditText)findViewById(R.id.poi_city);
		EditText zipcode = (EditText)findViewById(R.id.poi_zipcode);
		String beginDate = ((TextView)findViewById(R.id.poi_begin_date_value)).getText().toString();
		String endDate = ((TextView)findViewById(R.id.poi_end_date_value)).getText().toString();
		EditText desc = (EditText)findViewById(R.id.poi_description);
		EditText title = (EditText)findViewById(R.id.poi_new_title);
		boolean ispublic = !((CheckBox) findViewById(R.id.isprivate)).isSelected();
		Poi p = new Poi();
		p.city = city.getText().toString();
		p.description = desc.getText().toString();
		p.line1 = line1.getText().toString();
		p.line1 = line2.getText().toString();
		p.title = title.getText().toString();
		p.zipCode = zipcode.getText().toString();
//		if (!beginDate.equals(""))
//			p.begindatetime = "'"+beginDate+"'";
//		if (!endDate.equals(""))
//			p.enddatetime = "'"+endDate+"'";
		p.ispublic = (ispublic?1:0);
		Toast.makeText(PoiNew.this, "Please, wait while updating data...", Toast.LENGTH_SHORT).show();
		VampREST.addPoi(p);
		VampDatabase.getInstance(getApplicationContext()).getDbHelper().initPoi();
		goPoi();
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			goPoi();
			finish();
			return true;
		}
		return false;
	}
}
